/**
 *
 */
package com.wedroid.r2d2.message.parser;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.demo2do.core.utils.JsonUtils;
import com.wedroid.entity.message.MessageType;
import com.wedroid.r2d2.message.RawMessage;
import com.wedroid.r2d2.message.ShareInfo;
import com.wedroid.r2d2.resource.ResourceManager;

/**
 * @author lute
 */
@Component("shareContentParser")
public class ShareContentParser implements ContentParser {

    @Autowired
    private ResourceManager resourceManager;

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.message.parser.ContentParser#supportsMessageType(com.wedroid.entity.message.MessageType)
     */
    public boolean supportsMessageType(MessageType messageType) {
        return MessageType.SHARE.equals(messageType);
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.message.parser.ContentParser#parse(com.wedroid.r2d2.message.RawMessage, java.lang.String)
     */
    public String parse(RawMessage rawMessage, String escapedContent) {
        String msgId = rawMessage.getMsgId();
        Date createTime = new Date(rawMessage.getCreateTime() * 1000);
        String senderName = rawMessage.getFromUserName();

        String thumbUrl = resourceManager.downloadSlaveImage(msgId, createTime, senderName);
        ShareInfo shareInfo = new ShareInfo(rawMessage, escapedContent).thumb(thumbUrl);
        return JsonUtils.toJsonString(shareInfo);
    }

}
